<?php
require_once('phpmailer.php');

class MailUtil{
	private $Mail;

	function __construct($toaddress,$subject,$mailbody)
	{
		$this->Mail = new PHPMailer();
		$this -> AddAddress($toaddress);
		$this->Mail -> Subject = $subject;
		$this->Mail -> Body = $mailbody;
		//$this->Mail->AddAttachment("D:\a.txt","test-1.txt");
	}
	public function setCC($ccaddress,$ccname=null)
	{
		$cc = trim($ccaddress);
		if (!empty($ccaddress))
		{
			$this->Mail -> AddBCC($ccaddress,$ccname);
		}
	}
	function AddAddress($toAddress,$toname=null)
	{
		if(empty($toAddress) == true)
		{
			//throw new Exception('please input receive Address!');
		}
		if(is_array($toAddress) == true)
		{
			foreach ($toAddress as $address)
			{
				if(empty($address) ==true )
				{
					continue;
				}
				$this->Mail-> AddAddress($address);
			}
		}
		if(is_string($toAddress) == true)
		{
			$this->Mail-> AddAddress($toAddress,$toname);
		}
	}

	public function Send($myname="",$myemail="") {
		$email_params = Zend_Registry::get("email_params");
		$this->Mail -> IsHTML(true);
		$this->Mail ->Is_SSL = $email_params["ssl"];
		$this->Mail -> Mailer= 'smtp';
		$this->Mail -> CharSet = $email_params["charset"];          //default 'utf-8'
		$this->Mail -> Encoding = $email_params["encoding"];        //default 'base64'
		$this->Mail -> Host = $email_params["smtphost"];            //mail servers address
		$this->Mail -> Port = $email_params["port"];                //mail servers port
		$this->Mail -> From = $email_params["fromaddress"];         //addresser email
		$this->Mail -> ReplyTo[0][0] = $email_params["replymail"];
		$this->Mail -> ReplyTo[0][1] = $email_params["replyname"];
//		if(!empty($myname)){
//			$this->Mail -> FromName = $myname;
//		}else{
			$this->Mail -> FromName = $email_params["fromname"];        //addresser name
//		}
		$this->Mail -> SMTPAuth = $email_params["smtpauth"];        //mail servers status validate Yes: ture闂佹寧鍐婚幏锟� No: false
		$this->Mail -> Username = $email_params["mailusername"];    //mail servers name
		$this->Mail -> Password = $email_params["mailpassword"];    //mail servers password
		$this->Mail -> WordWrap = $email_params["wordwrap"];
		if (!$this->Mail -> Send()) {
			//            throw new Exception('send mail err!');
		}
		return  true;
	}

	public static function sendMail($Email,$Title,$Body){

		//        $Mailinfo = MailUtil::getMail($mailtype);
		//
		//        $Body = MailUtil::formatEmail($Mailinfo['Body'],$data,$age,'n');

		$Mail = new MailUtil($Email,$Title,$Body);
		$Mail->Send();
	}
}
?>